package com.demo.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.comparator.CompareUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.entity.TTransfer;
import com.demo.entity.TWorkorder;
import com.demo.mapper.TTransferMapper;
import com.demo.mapper.TWorkorderMapper;
import com.demo.service.ITWorkorderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.vo.TTAndTWVoDemo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-08-02
 */
@Service
@Transactional
@Slf4j
public class TWorkorderServiceImpl extends ServiceImpl<TWorkorderMapper, TWorkorder> implements ITWorkorderService {

    @Autowired
    private TWorkorderMapper workorderMapper;
    @Autowired
    private TTransferMapper tTransferMapper;

    @Override
    public List<TWorkorder> selcetList() {
        List<TWorkorder> tWorkorderList = workorderMapper.selectList(null);
        return tWorkorderList;
    }

    @Override
    public IPage<TWorkorder> selectPageDemo(Integer pageNum, Integer pageSize) {
        Page<TWorkorder> tWorkorderPage = new Page<>(pageNum , pageSize);
        IPage<TWorkorder> tWorkorderIPage = workorderMapper.selectPage(tWorkorderPage, null);
        return tWorkorderIPage;
    }

    public static void main(String[] args) {
        if (CompareUtil.compare(
                DateUtil.parse("2021-11-01 00:00:00", "yyyy-MM-dd HH:mm:ss"),
                DateUtil.parse("2022-07-31 23:59:59", "yyyy-MM-dd HH:mm:ss"))>0){
            Console.log("ok");
        }else {
            Console.log("nook");
        }
    }
    @Override
    public List<TTAndTWVoDemo> selectGetTWAndTT() {
        List<TTAndTWVoDemo> ttAndTWVoDemoList = new ArrayList<TTAndTWVoDemo>();
        List<TTAndTWVoDemo> ttAndTWVoDemoList1 = new ArrayList<>();
        TimeInterval timer = DateUtil.timer();
        LambdaQueryWrapper<TWorkorder> wrapper = new QueryWrapper<TWorkorder>().lambda()
                .gt(TWorkorder::getCtiem,"2021-11-01 00:00:00")
                .eq(TWorkorder::getWosource,"0");
        List<TWorkorder> tWorkorderList = workorderMapper.selectList(wrapper);
        Console.log("数据库查询数据所用时间：",timer.intervalSecond());
        for (TWorkorder tWorkorder:tWorkorderList) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(tWorkorder.getCtiem());
            if (CompareUtil.compare(
                    DateUtil.parse(stringBuffer, "yyyy-MM-dd HH:mm:ss"),
                    DateUtil.parse("2022-07-31 23:59:59", "yyyy-MM-dd HH:mm:ss"),
                    false)<0){
                TTAndTWVoDemo build = TTAndTWVoDemo.builder()
                        .wno(tWorkorder.getWno())
                        .ctiem(tWorkorder.getCtiem())
                        .wosource(tWorkorder.getWosource())
                        .build();
                ttAndTWVoDemoList.add(build);
            };
        }
        Console.log("经过判断写入数据时间：",timer.intervalSecond());
        ttAndTWVoDemoList.forEach(a->{
            LambdaQueryWrapper<TTransfer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TTransfer::getWno,a.getWno())
                    .eq(TTransfer::getOusername,"朝阳区供热办");
            TTransfer first = CollUtil.getFirst(tTransferMapper.selectList(queryWrapper));
            if (ObjectUtil.isNotNull(first)){
                TTAndTWVoDemo build = TTAndTWVoDemo.builder()
                        .id(first.getId())
                        .ousername(first.getOusername())
                        .wno(a.getWno())
                        .ctiem(a.getCtiem())
                        .wosource(a.getWosource())
                        .build();
                ttAndTWVoDemoList1.add(build);
            }
        });

        Console.log("再次操作数据库并填入数据：",timer.intervalSecond());
        log.info("成功了");
        return ttAndTWVoDemoList1;
    }
}
